package com.itxzh.poidemo.controller;

import com.itxzh.common.entities.Person;
import com.itxzh.poidemo.service.PersonService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.Duration;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Description
 *
 * @Author ldt
 * @Date 2022/5/12 9:38
 * @Version 1.0.0
 */
@RestController
@RequestMapping("/person")
@Slf4j
public class PersonController {

    @Autowired
    private PersonService personService;

    @RequestMapping("/getAll")
    public List<Person> getAll() {
        List<Person> list = personService.getAll();
        return list;
    }

    @RequestMapping("/create")
    public String create(Person person) {
        int res = personService.create(person);
        return "success";
    }

    /**
     * 批量插入：foreach标签
     * @return
     */
    @RequestMapping("/foreachInsert")
    public Map<String, Object> foreachInsert() {
        LocalDateTime start = LocalDateTime.now();
        log.info("start=" + start);
        List<Person> temp = new ArrayList<>();
        for (int i = 0; i < 100000; i++) {
            Person person1 = new Person();
            person1.setName("foreach"+i);
            person1.setAge(20);
            person1.setSex(i%2==0?"M":"F");
            temp.add(person1);
            if(temp.size()==100) {
                personService.insertBatch(temp);
                temp.clear();
            }
        }
        if(temp.size() > 0) {
            personService.insertBatch(temp);
            temp.clear();
        }
        LocalDateTime end = LocalDateTime.now();
        log.info("end=" + end);
        long cost = Duration.between(start, end).getSeconds();
        log.info("cost(second)=" + cost);

        Map<String, Object> result = new HashMap<>();
        result.put("start", start);
        result.put("end", end);
        result.put("cost(second)=", cost);
        return result;
    }

    /**
     * 批量插入：循环插入
     * @return
     */
    @RequestMapping("/forCreate")
    public Map<String, Object> forCreate() {
        LocalDateTime start = LocalDateTime.now();
        log.info("start=" + start);
        for (int i = 0; i < 100000; i++) {
            Person person1 = new Person();
            person1.setName("for"+i);
            person1.setAge(20);
            person1.setSex(i%2==0?"M":"F");
            personService.create(person1);
        }
        LocalDateTime end = LocalDateTime.now();
        log.info("end=" + end);
        long cost = Duration.between(start, end).getSeconds();
        log.info("cost(second)=" + cost);

        Map<String, Object> result = new HashMap<>();
        result.put("start", start);
        result.put("end", end);
        result.put("cost(second)=", cost);
        return result;
    }
}
